package br.com.pentagrama.service.impl;

import java.io.BufferedInputStream;
import java.io.IOException;

public class DownloadThread extends Thread {

	private int BUFFER_SIZE = 4096;
	
	private BufferedInputStream in = null;
	
	private long tamanho = 0;
	
	/**
	 * Método "get" para o atributo tamanho.
	 *
	 * @return Atributo tamanho
	 */
	public long getTamanho() {
		return tamanho;
	}

	public DownloadThread(BufferedInputStream in) {
		this.in = in;
	}
	
	public void run() {

		try {
			
			byte data[] = new byte[BUFFER_SIZE];
			int numRead;		
		
			while (((numRead = in.read(data, 0, BUFFER_SIZE)) != -1)) {
				// write to buffer
				tamanho += numRead;
			}
				

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
	}

	/**
	 * Método "get" para o atributo in.
	 *
	 * @return Atributo in
	 */
	public BufferedInputStream getIn() {
		return in;
	}

	/**
	 * Método "set" para o atibuto in.
	 *
	 * @param in utilizado para atualizar o atributo: in
	 */
	public void setIn(BufferedInputStream in) {
		this.in = in;
	}

}
